package socket;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class Client {
    private Socket socket;
    public Client(){
        try {
            System.out.println("客户端启动...");
            //socket = new Socket("172.100.0.104",8088);
            socket = new Socket("localhost",8088);
            System.out.println("客户端启动成功...");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void start(){
        try {
            OutputStream out = socket.getOutputStream();
            Scanner scan = new Scanner(System.in);
            while (true) {
                String line = scan.nextLine();
                if("exit".equals(line)){
                    break;
                }
                byte[] data = line.getBytes(StandardCharsets.UTF_8);
                out.write(data.length);
                out.write(data);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try{
                if(socket != null){
                    socket.close();
                }
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Client client = new Client();
        client.start();
    }
}
